<?php
define('APPNEWS','1.00');
/************************************************************************/
/* FrameWork  test preview                                              */
/* ==================================================================== */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/


//generatore di RSSFeed (estratto e rielaborato da flatnnuke)

function create_newsRSS($datas){

    $appname=$_GET['appname'];
    $apptitle=apptitle($appname);
    $sitename = title();
    $admin_mail = get_config("gestione_mails/admin_mail","");
    $newspp = newsPP("admin/",$apptitle);

    $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
    $url = str_replace(basename($url),"",$url);

    // tag apertura del feed
    $body = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<rss version=\"2.0\">\n\t<channel>\n";
    // informazioni generali sul feed
    $body .= "\t\t<title>$sitename</title>\n\t\t<link>$url</link>\n\t\t<description><![CDATA[\"$sitename\" HEADLINES]]></description>\n";
    $body .= "\t\t<managingEditor>$admin_mail</managingEditor>\n\t\t<generator>FrameWork RSS Generator - http://www.framework.altervista.org</generator>\n";
    $body .= "\t\t<lastBuildDate>".date("D, d M Y H:i:s",time())." GMT</lastBuildDate>\n";
    // carico array con le news ordinate per data
    $handle = opendir($datas);
    $modlist = array();
    while ($file = readdir($handle)){
	if ($file[0]!="." )
	    array_push($modlist, $file);
    }
    closedir($handle);

    if(count($modlist)>0)
	rsort($modlist);
    // creazione tag per ognuna delle news
    for ($i=0; $i < count($modlist); $i++) {
	if ($i>=$newspp)
	    break;
	$string = get_file("$datas/$modlist[$i]");
	$mytitle = get_xml_element("fn:title",$string);
	$mydesc = str_replace("<br>","<br />",get_xml_element("fn:header",$string));
	$id = str_replace(".xml","",$modlist[$i]);
	$body .= "\t\t<item>\n";
	$body .= "\t\t\t<title>$mytitle</title>\n";
	$body .= "\t\t\t<link>$url"."index.php?appname=$appname&amp;op=view_news&amp;file=$id.xml</link>\n\t\t\t<description><![CDATA[$mydesc]]></description>\n";
	$body .= "\t\t\t<pubDate>".date("D, d M Y H:i:s",$id)." GMT</pubDate>\n";
	$body .= "\t\t</item>\n";
    }
    // tag chiusura del feed
    $body.="\t</channel>\n</rss>";
    // scrittura del feed
    $handle=fopen("datas/applications/$apptitle/backend.xml","w");
    fwrite($handle,$body);
    fclose($handle);
}
//------------------------------------------------------------------------------------------------------------------------

// Configure - functions

function newsPP($path,$apptitle){
//$r= join(file("datas/$path/$apptitle/config/newsPP"))+0;
$r=get_config("applications/$apptitle/newsPP","5");
return $r;
}

function commentFlag($path,$apptitle){
    if (get_config("applications/$apptitle/b-commentFlag","")!="")
	return true;
    else
	return false;
}

//------------------------------------------------------------------------------------------------------------------------
// Configure - interfaccia di amministrazione

function admin($path,$appname){
    if (admin_isadmin()){
	echo "<b>"._ADMIN_NEWS."</b>";
	echo "<form name='admin' action='index.php?appname=$appname&op=exec_admin' method='POST' >\n";
	echo _NEWSPP." <input type='text' name='newspp' value='".newsPP($path,apptitle($appname))."' size='2' /><br />\n";
	if (commentFlag($path,apptitle($appname))==false)
	    echo _COMMENTFLAG." <input type='checkbox' name='commentflag' /><br />\n";
	else
	    echo _COMMENTFLAG." <input type='checkbox' name='commentflag' checked='1' /><br />\n";

	echo "<input type='submit' value='save'>";
	echo "</form>\n";


	echo "<hr/><b>"._ADMIN_ARGS."</b>";
	echo "<table class='' >\n";
	$fd=opendir("datas/applications/".apptitle($appname)."/arguments/");
	while (false !== ($nf= readdir($fd))){
	    if ($nf[0]!='.' ){
		echo "<tr valign='center'>";
		echo "<td ><img src='datas/applications/".apptitle($appname)."/arguments/$nf' width='24'><br/></td>";
		$f=explode(".",$nf);
		echo "<td>";
		echo "<form action='index.php?appname=$appname&op=exec_admin&subop=renimage' method='post'><input type='hidden' name='name' value='$nf' /><input type='text' name='newname' value='".$f[0]."'/><input type='submit' value='rename' /></form>";
		echo "</td>";
		echo "<td>";
		echo "<form action='index.php?appname=$appname&op=exec_admin&subop=delimage' method='post'><input type='hidden' name='image' value='$nf' /> <input type='submit' value='delete' /></form>";
		echo "</td>";
		echo "</tr>\n";
	    }
	}
	closedir($fd);
	echo "</table>\n";
	echo "<form name=\"imageupload\" action=\"index.php?appname=$appname&op=exec_admin&subop=upload\" enctype='multipart/form-data' method='post'>";
	echo _NUOVO_ARGOMENTO."<input type=\"file\" name=\"image\"><input type=submit value=\"load\"></form>\n";



    }
}

function exec_admin($path,$appname){
    if (admin_isadmin()){
	admin_writelog("news:admin modify");
	$subop=$_GET['subop'];
	switch ($subop){
	    case "upload":
	    if (move_uploaded_file($_FILES['image']['tmp_name'], "datas/applications/".apptitle($appname)."/arguments/".$_FILES['image']['name'])) {
		//echo $_FILES['image']['name'];
		if (strtolower(substr($_FILES['image']['name'],-4))==".php" )
		    unlink("datas/applications/".apptitle($appname)."/arguments/".$_FILES['image']['name']);
		else{
		    echo "<script>alert('"._ICONA_ARGOMENTO.$_FILES['image']['name']._CARICATO."');</script>";
		}
	    }
	    break;

	    case "delimage":
	    $image=$_POST['image'];
	    if (file_exists("datas/applications/".apptitle($appname)."/arguments/".$image)){
		unlink("datas/applications/".apptitle($appname)."/arguments/".$image);
		echo "<script>alert('"._ICONA_ARGOMENTO."$image"._ELIMINATO."');</script>";
	    }
	    break;

	    case "renimage":
	    $name=$_POST['name'];
	    $newname=$_POST['newname'];
	    $f=explode(".",$name);
	    $newname=$newname.".".$f[1];
	    if (file_exists("datas/applications/".apptitle($appname)."/arguments/".$name)){
		rename("datas/applications/".apptitle($appname)."/arguments/".$name,"datas/applications/".apptitle($appname)."/arguments/".$newname);
		echo "<script>alert('"._ICONA_ARGOMENTO."$name"._RINOMINATO_IN."$newname');</script>";
	    }
	    break;

	    default:
	    $apptitle=apptitle($appname);
	    $newspp=$_POST['newspp'];
	    $commentflag=$_POST['commentflag'];

	    set_config("applications/$apptitle/newsPP",$newspp);
	    set_config("applications/$apptitle/b-commentFlag",$commentflag);

	    echo "<b>"._ADMIN_NEWS_AGGIORNATO."</b><br /><br />";

	    echo " "._NEWSPP." $newspp<br />\n";
	    if ($commentflag!="")echo " ". _COMMENTFLAG."<br />\n";
	    break;
	}

	echo "<h3 class='edit'><a href='index.php?appname=$appname&op=admin'>"._CONTINUA."</a></h3>";
	echo "<meta http-equiv=\"Refresh\" content=\"0; URL=".$siteurl."index.php?appname=$appname&op=admin\">";

	echo"<hr />";
    }
}

//------------------------------------------------------------------------------------------------------------------------


// carica libemoticons e libBBcode se sono presenti
loadlib("libBBcode");
loadlib("libEmoticons");

//carica la lingua se esiste
$lang=lang();
if (file_exists("$path/$appname/lang/$lang.inc"))
    include "$path/$appname/lang/$lang.inc";
else
    include "$path/$appname/lang/it.inc";


echo "
<script language=\"javascript\">
function preview(){
document.prev.data_ora.value=document.insnews.data_ora.value;
document.prev.data_minuto.value=document.insnews.data_minuto.value;
document.prev.data_mese.value=document.insnews.data_mese.value;
document.prev.data_giorno.value=document.insnews.data_giorno.value;
document.prev.data_anno.value=document.insnews.data_anno.value;
document.prev.title.value=document.insnews.title.value;
document.prev.icon.value=document.insnews.icon.value;
";
if (file_exists("extras/FCKeditor/fckeditor.php")&& get_config("applications/".apptitle($appname)."/b-fcknews","on")!=""){
    echo"
    var oEditorHead = FCKeditorAPI.GetInstance('head') ;
    document.prev.head.value=oEditorHead.GetHTML();
    var oEditorBody = FCKeditorAPI.GetInstance('body') ;
    document.prev.body.value=oEditorBody.GetHTML();
    ";
}
else{
    echo"
    document.prev.head.value=document.insnews.head.value;
    document.prev.body.value=document.insnews.body.value;
";
}

echo"
}
</script>
";

//-------------------------------------------------------------------------------------------------------------------------
function count_comment_news($name){
	$string=join(file("$name"));
	$comments=ereg_replace(".*<fn:comments>","",$string);
	$comments=ereg_replace("</fn:comments>.*","",$comments);

	$comment= explode("</fn:comment>",$comments);
	return count($comment)-1;
}
//-------------------------------------------------------------------------------------------------------------------------

function comment_news($news,$path,$avatar_path,$appname){
	$name=$_GET['file'];
	$apptitle=apptitle($appname);
	$name=str_replace("..","",$name);
	if(!file_exists("datas/$path/$apptitle/news/$name"))return;
	$string=join(file("datas/$path/$apptitle/news/$name"));
	//echo "commenti $name:".$string;

	print "<h3>"._ADDCOMM."</h3>";
	$title=ereg_replace(".*<fn:title>","",$string);
	$title=ereg_replace("</fn:title>.*","",$title);
	$ntile=$title;

	$header=ereg_replace(".*<fn:header>","",$string);
	$header=ereg_replace("</fn:header>.*","",$header);

	$reads=ereg_replace(".*<fn:reads>","",$string);
	$reads=ereg_replace("</fn:reads>.*","",$reads);

	$avatar=ereg_replace(".*<fn:avatar>","",$string);
	$avatar=ereg_replace("</fn:avatar>.*","",$avatar);

	$body=ereg_replace(".*<fn:body>","",$string);
	$body=ereg_replace("</fn:body>.*","",$body);

	$comments=ereg_replace(".*<fn:comments>","",$string);
	$comments=ereg_replace("</fn:comments>.*","",$comments);

	$comment= explode("</fn:comment>",$comments);
	array_splice ($comment, count($comment)-1);


	if (commentFlag($path,apptitle($appname)) ||user_getuser()!=""){
	    // form di aggiunta commento
	    $user=user_getuser();
	    if ($user=="")$user="unknow";
	    echo "
	<form name='add_comment' action='index.php?appname=$appname&op=exec_comment_news&file=$name' method='POST' >
	    <input type='hidden' name='by' value='$user' />
	    <input type='hidden' name='date' value='".time()."' />";
	    if (file_exists("extras/FCKeditor/fckeditor.php")&& get_config("applications/".apptitle($appname)."/b-fcknews","on")!=""){
		$sBasePath ="extras/FCKeditor/";
		$oFCKeditorPost = new FCKeditor('post');
		$oFCKeditorPost->BasePath	= $sBasePath;
		$oFCKeditorPost->Height= 400;
		//$oFCKeditorPost->Value	= @join(@file($file));
		$oFCKeditorPost->Create();
	    }
	    else{
		if (defined('LIBBBCODE'))creatoolbar("document.add_comment.post");
		echo "
		<textarea name='post'style=\" width:100%; height:150px; \"></textarea><br />";
	    }
	    echo "
		<input type='submit' value='"._ADDCOMM."' />
		</form>
		<hr />
		";
	}
	//visualizza la notizia

	echo "<table id='news' class='class_table'>";
	echo "<td colspan='2'><h3>$ntitle</h3></td>";
	echo "<tr>";
	echo "<td width=10><img border='1' alt='$avatar' src='$avatar_path/$avatar'></td>";
	echo "<td>$header</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td colspan='2'>$body</td>";
	echo "</tr>";
	echo "</table><br/>";

	echo "<center>"._POSTATO.": ".date(formato_data_ora(),substr($name,-14,-4))."</center>";

	echo"<hr /><hr />";


	//visualizza i commenti
	for($i=0; $i<count($comment);$i++){
	    $by=ereg_replace(".*<fn:by>","",$comment[$i]);
	    $by=ereg_replace("</fn:by>.*","",$by);

	    $date=ereg_replace(".*<fn:date>","",$comment[$i]);
	    $date=ereg_replace("</fn:date>.*","",$date);

	    $post=ereg_replace(".*<fn:post>","",$comment[$i]);
	    $post=ereg_replace("</fn:post>.*","",$post);

	    echo"
	     "._DA." <b>$by</b>  "._DATA." <i>".date(formato_data_ora(),$date)."</i>  <br /><br />
	    $post <br />
	    <hr />
	    ";
	    if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()){
		echo "<h3 class='edit'>";
		echo "<a href=\"index.php?appname=$appname&file=$name&num_comment=$i&op=modify_comment_news\">"._MODCOMM."</a>";
		echo " | ";
		echo "<a href=".ahah_link("index.php?appname=$appname&file=$name&num_comment=$i&op=delete_comment_news",$path,"center").">"._DELCOMM."</a>";
		echo "</h3>";
		echo "<hr/>";
	    }
	}
}

function exec_comment_news($news,$path,$avatar_path,$appname){
    if (commentFlag($path,apptitle($appname)) ||user_getuser()!=""){
	$apptitle=apptitle($appname);
	$by=stripslashes(htmlspecialchars($_POST['by']));
	$date=$_POST['date'];

	if (file_exists("extras/FCKeditor/fckeditor.php")&& get_config("applications/".apptitle($appname)."/b-fcknews","on")!="")
	    $post=stripslashes($_POST['post']);
	else
	    $post=stripslashes(htmlspecialchars($_POST['post']));
	$post=str_replace("\r","",$post);
	$post=str_replace("\n","<br>",$post);
	$post=str_replace("<script","",$post);
	$post=str_replace("</script","",$post);
	$post=str_replace("<?","",$post);
	$post=str_replace("?>","",$post);
	if (defined('LIBBBCODE'))$post=bb2html($post);
	if (defined('LIBEMOTICONS'))$post=emoticons2html($post);


	$name=$_GET['file'];
	
	$name=str_replace("..","",$name);

	$string=join(file("datas/$path/$apptitle/news/$name"));

	// primo commento
	if(!stristr($string,"<fn:comments>")){
	    $string=str_replace("</fn:news>","\t<fn:comments>\n\t\t<fn:comment>\n\t\t\t<fn:by>$by</fn:by>\n\t\t\t<fn:date>$date</fn:date>\n\t\t\t<fn:post>$post</fn:post>\n\t\t</fn:comment>\n\t</fn:comments>\n</fn:news>",$string);
	}
	// c'� almeno un commento
	else {
	    $string=str_replace("</fn:comments>","\t<fn:comment>\n\t\t\t<fn:by>$by</fn:by>\n\t\t\t<fn:date>$date</fn:date>\n\t\t\t<fn:post>$post</fn:post>\n\t\t</fn:comment>\n\t</fn:comments>",$string);
	}

	$fp=fopen("datas/$path/$apptitle/news/$name","w");
	fwrite($fp,"$string");
	fclose($fp);
    }
    echo "<b>"._COMMENTO_INSERITO."</b><br />";
    echo "<h3 class='edit'><a href='index.php?appname=$appname'>"._CONTINUA."</a></h3>";
    echo"<hr />";

}
//-------------------------------------------------------------------------------------------------------------------------

// modifica commento news
function modify_comment_news($news,$path,$avatar_path,$appname){
    $name=$_GET['file'];
    $apptitle=apptitle($appname);
    $name=str_replace("..","",$name);

    $num_comment=$_GET['num_comment'];

    $string=join(file("datas/$path/$apptitle/news/$name"));
    //echo "commenti $name:".$string;

    print "<h3>"._ADDCOMM."</h3>";
    $title=ereg_replace(".*<fn:title>","",$string);
    $title=ereg_replace("</fn:title>.*","",$title);
    $ntile=$title;

    $header=ereg_replace(".*<fn:header>","",$string);
    $header=ereg_replace("</fn:header>.*","",$header);

    $reads=ereg_replace(".*<fn:reads>","",$string);
    $reads=ereg_replace("</fn:reads>.*","",$reads);

    $avatar=ereg_replace(".*<fn:avatar>","",$string);
    $avatar=ereg_replace("</fn:avatar>.*","",$avatar);

    $body=ereg_replace(".*<fn:body>","",$string);
    $body=ereg_replace("</fn:body>.*","",$body);

    $comments=ereg_replace(".*<fn:comments>","",$string);
    $comments=ereg_replace("</fn:comments>.*","",$comments);

    $comment= explode("</fn:comment>",$comments);
    array_splice ($comment, count($comment)-1);


    //creo i campi da modificare del commento;
    $modify_comment=$comment[$num_comment];

    $by=ereg_replace(".*<fn:by>","",$modify_comment);
    $by=ereg_replace("</fn:by>.*","",$by);

    $date=ereg_replace(".*<fn:date>","",$modify_comment);
    $date=ereg_replace("</fn:date>.*","",$date);

    $post=ereg_replace(".*<fn:post>","",$modify_comment);
    $post=ereg_replace("</fn:post>.*","",$post);

    if (defined('LIBEMOTICONS'))$post=html2emoticons($post);
    if (defined('LIBBBCODE'))$post=html2bb($post);



    // form di aggiunta commento
    $user=user_getuser();
    if ($user=="")$user="unknow";
	echo "
	<form name='modify_comment' action='index.php?appname=$appname&op=exec_modify_comment_news&file=$name' method='POST' >
	    <input type='hidden' name='num_comment' value='$num_comment' />
	    <input type='hidden' name='by' value='$by' />
	    <input type='hidden' name='date' value='$date' />";
    if (file_exists("extras/FCKeditor/fckeditor.php")&& get_config("applications/".apptitle($appname)."/b-fcknews","on")!=""){
	$sBasePath ="extras/FCKeditor/";
	$oFCKeditorPostMod = new FCKeditor('post');
	$oFCKeditorPostMod->BasePath	= $sBasePath;
	$oFCKeditorPostMod->Height= 400;
	$oFCKeditorPostMod->Value	= $post;
	$oFCKeditorPostMod->Create();
    }
    else{
	if (defined('LIBBBCODE'))creatoolbar("document.modify_comment.post");
	echo "
	    <textarea name='post'style=\" width:100%; height:150px; \">$post</textarea><br />";
    }
    echo "
	<input type='submit' value='"._MODCOMM."' />
	</form>
	<hr />
    ";

    //visualizza la notizia

    echo "<table id='news' class='class_table'>";
    echo "<td colspan='2'><h3>$ntitle</h3></td>";
    echo "<tr>";
    echo "<td width=10><img border='1' alt='$avatar' src='$avatar_path/$avatar'></td>";
    echo "<td>$header</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td colspan='2'>$body</td>";
    echo "</tr>";
    echo "</table><br/>";

    echo "<center>"._POSTATO.": ".date(formato_data_ora(),substr($name,-14,-4))." </center>";

    echo"<hr /><hr />";


    //visualizza i commenti
    for($i=0; $i<count($comment);$i++){
	$by=ereg_replace(".*<fn:by>","",$comment[$i]);
	$by=ereg_replace("</fn:by>.*","",$by);

	$date=ereg_replace(".*<fn:date>","",$comment[$i]);
	$date=ereg_replace("</fn:date>.*","",$date);

	$post=ereg_replace(".*<fn:post>","",$comment[$i]);
	$post=ereg_replace("</fn:post>.*","",$post);

	echo "
	    "._DA." <b>$by</b>  "._DATA." <i>".date(formato_data_ora(),$date)."</i>  <br /><br />
	    $post <br />
	    <hr />
	    ";
	if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()){
	    echo "<h3 class='edit'>";
	    echo "<a href=\"index.php?appname=$appname&file=$name&num_comment=$i&op=modify_comment_news\">"._MODCOMM."</a>";
	    echo " | ";
	    echo "<a href=".ahah_link("index.php?appname=$appname&file=$name&num_comment=$i&op=delete_comment_news",$path,"center").">"._DELCOMM."</a>";
	    echo "</h3>";
	    echo "<hr/>";
	}
    }
}

function exec_modify_comment_news($news,$path,$avatar_path,$appname){
    if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()){

	admin_writelog("news:modify comment news");
	$apptitle=apptitle($appname);
	$num_comment=$_POST['num_comment'];
	$by=stripslashes(htmlspecialchars($_POST['by']));
	$date=$_POST['date'];

	if (file_exists("extras/FCKeditor/fckeditor.php")&& get_config("applications/".apptitle($appname)."/b-fcknews","on")!="")
	    $post=stripslashes($_POST['post']);
	else
	    $post=stripslashes(htmlspecialchars($_POST['post']));
	$post=str_replace("\r","",$post);
	$post=str_replace("\n","<br>",$post);
	$post=str_replace("<script","",$post);
	$post=str_replace("</script","",$post);
	$post=str_replace("<?","",$post);
	$post=str_replace("?>","",$post);
	if (defined('LIBBBCODE'))$post=bb2html($post);
	if (defined('LIBEMOTICONS'))$post=emoticons2html($post);

	$name=$_GET['file'];
	$name=str_replace("..","",$name);

	$string=join(file("datas/$path/$apptitle/news/$name"));

	$comments=ereg_replace(".*<fn:comments>","",$string);
	$comments=ereg_replace("</fn:comments>.*","",$comments);

	$comment= explode("</fn:comment>",$comments);
	array_splice ($comment, count($comment)-1);

	$string=str_replace($comment[$num_comment]."</fn:comment>","\t<fn:comment>\n\t\t\t<fn:by>$by</fn:by>\n\t\t\t<fn:date>$date</fn:date>\n\t\t\t<fn:post>$post</fn:post>\n\t\t</fn:comment>\n",$string);

	$fp=fopen("datas/$path/$apptitle/news/$name","w");
	fwrite($fp,"$string");
	fclose($fp);

	echo "<b>"._COMMENTO_MODIFICATO."</b><br />";
	echo "<h3 class='edit'><a href='index.php?appname=$appname'>"._CONTINUA."</a></h3>";
	echo"<hr />";
    }
}
//-------------------------------------------------------------------------------------------------------------------------

//elimina commento news
function delete_comment_news($news,$path,$avatar_path,$appname){
    if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()){
	admin_writelog("news:delete comment news");
	$apptitle=apptitle($appname);

	$num_comment=$_GET['num_comment'];
	$name=$_GET['file'];
	$name=str_replace("..","",$name);

	$string=join(file("datas/$path/$apptitle/news/$name"));

	$comments=ereg_replace(".*<fn:comments>","",$string);
	$comments=ereg_replace("</fn:comments>.*","",$comments);

	$comment= explode("</fn:comment>",$comments);
	array_splice ($comment, count($comment)-1);

	$string=str_replace($comment[$num_comment]."</fn:comment>","",$string);

	$fp=fopen("datas/$path/$apptitle/news/$name","w");
	fwrite($fp,"$string");
	fclose($fp);

	echo "<b>"._COMMENTO_ELIMINATO."</b><br />";
	echo "<h3 class='edit'><a href='index.php?appname=$appname'>"._CONTINUA."</a></h3>";
	echo"<hr />";
    }
}
//-------------------------------------------------------------------------------------------------------------------------


function create_news($news,$path,$avatar_path,$appname){
    if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()){
	echo "
<form name='insnews' action=\"index.php?appname=$appname&op=exec_create_news\" method=\"POST\" >
<input type=\"hidden\" name=\"mod\" value=\"signews\" />
<b>"._DATANOTIZIA."</b><br />
    ";
	echo "<select name=\"data_giorno\" >";
	echo "<option>".date("d")."</option>";
	for ($i=1;$i<=31;$i++)
	    echo "<option>".substr("00".$i,-2)."</option>";
	echo "</select>";

	echo "<select name=\"data_mese\" >";
	echo "<option>".date("m")."</option>";
	for ($i=1;$i<=12;$i++)
	    echo "<option>".substr("00".$i,-2)."</option>";
	echo "</select>";

	echo "<select name=\"data_anno\" >";
	echo "<option>".date("Y")."</option>";
	for ($i=2006;$i<=2012;$i++)
	    echo "<option>".substr("0000".$i,-4)."</option>";
	echo "</select>";

	echo " : ";

	echo "<select name=\"data_ora\" >";
	echo "<option>".date("H")."</option>";
	for ($i=0;$i<=23;$i++)
	    echo "<option>".substr("00".$i,-2)."</option>";
	echo "</select>";

	echo "<select name=\"data_minuto\" >";
	echo "<option>".date("i")."</option>";
	for ($i=0;$i<60;$i++)
	    echo "<option>".substr("00".$i,-2)."</option>";
	echo "</select>";

	echo "<br /><br />
<b>"._TITNOTIZIA."</b><br />
<input type=\"text\" name=\"title\" /><br /><br />
<b>"._ARGOMENTO."</b><br />
<img name=\"avatar\" src=\"$avatar_path/nonews.png\" border=\"0\" alt=\"Argomento\" />
<br />
<select name=\"icon\" onChange='document.insnews.avatar.src=\"$avatar_path/\"+this.options[this.selectedIndex].value'>
<option></option>
";
	$handle=opendir($avatar_path);
	while ($file = readdir($handle)) {
	    if (!( $file=="." or $file==".." )) {
		$mymodlist .= "$file ";
	    }
	}
	closedir($handle);
	$mymodlist = explode(" ", $mymodlist);
	sort($mymodlist);
	for ($i=1; $i < sizeof($mymodlist); $i++) {
	    echo "<option value=\"$mymodlist[$i]\">".ereg_replace("\..*","",$mymodlist[$i])."</option>\n";
	}
	echo "
</select>
<br /><br /> ";
	echo "
<b>"._INTMESSAGGIO."</b><br />";
	if (file_exists("extras/FCKeditor/fckeditor.php")&& get_config("applications/".apptitle($appname)."/b-fcknews","on")!=""){

	    $sBasePath ="extras/FCKeditor/" ;

	    $oFCKeditorhead = new FCKeditor('head') ;
	    $oFCKeditorhead->BasePath	= $sBasePath;
	    $oFCKeditorhead->Height= 400;
	    $oFCKeditorhead->Create() ;
	}
	else{

	    if (defined('LIBBBCODE'))creatoolbar("document.insnews.head");
	    echo "
<textarea name='head'  style=\"width:100%;height:300px;\" ></textarea><br /><br />\n";
	}
	echo "
<b>"._CORPOMESSAGGIO."</b> <br />";
	if (file_exists("extras/FCKeditor/fckeditor.php")&& get_config("applications/".apptitle($appname)."/b-fcknews","on")!=""){

	    $oFCKeditorbody = new FCKeditor("body") ;
	    $oFCKeditorbody->BasePath	= $sBasePath;
	    $oFCKeditorbody->Height= 400;
	    $oFCKeditorbody->Create() ;
	}
	else{
	    if (defined('LIBBBCODE'))creatoolbar("document.insnews.body");
	    echo "
<textarea name='body'  style=\"width:100%;height:300px;\"></textarea>\n";
	}
	echo "
<input type=\"hidden\" name=\"mionome\" value=\"".$comfrom."\"><br /><br />
<input type=\"submit\" value=\""._INSNOTIZIA."\">
</form>";
	echo "<form name=\"prev\" action=\"index.php?appname=$appname&op=exec_preview_news\"  method=\"POST\">
<input type=\"hidden\" name=\"data_ora\" >
<input type=\"hidden\" name=\"data_minuto\" >
<input type=\"hidden\" name=\"data_mese\" >
<input type=\"hidden\" name=\"data_giorno\" >
<input type=\"hidden\" name=\"data_anno\" >
<input type=\"hidden\" name=\"mod\" value=\"prev\">
<input type=\"hidden\" name=\"title\">
<input type=\"hidden\" name=\"icon\">
<input type=\"hidden\" name=\"head\">
<input type=\"hidden\" name=\"body\">
<input type=\"submit\" value=\""._ANTEPRIMA."\" onMouseOver='preview();'>

</form>
";
	$pic_path="datas/$path/".apptitle($appname)."/newsimages";
	if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()){
	    $subop=$_GET['subop'];
	    if ($subop=="upload"){
		if (move_uploaded_file($_FILES['image']['tmp_name'], $pic_path."/".$_FILES['image']['name'])) {
		    //echo $_FILES['image']['name'];
		    if (strtolower(substr($_FILES['image']['name'],-4))==".php" )
			unlink($pic_path."/".$_FILES['image']['name']);
		    else{
			echo "<script>alert('immagine ".$_FILES['image']['name']." caricata');</script>";
		    }
		}
	    }
	    if ($subop=="delimage"){
		$image=$_POST['image'];
		if (file_exists($pic_path."/".$image)){
		    unlink($pic_path."/".$image);
		    echo "<script>alert('immagine $image eliminata');</script>";
		}
	    }
?>

    <hr />
    <form name="images_upload" action="index.php?appname=<?=$appname?>&op=create_news&subop=upload" enctype='multipart/form-data' method='post'>
    carica un immagine<input type="file" name="image"><input type=submit value="load">
    </form>

    <form name="images" action="index.php?appname=<?=$appname?>&op=create_news&subop=delimage" enctype='multipart/form-data' method='POST'>
    <select name="image">

    <?php
	//library auto load
	$i=0;
	$fd=opendir("$pic_path");
	while (false !== ($nf= readdir($fd))){
	    if ($nf[0]!='.' )
		$images[$i++]=$nf;
	}
	closedir($fd);
	if ($i>0){
	    sort($images);
	    foreach($images as $image){
		echo"<option>$image</option>";
	    }
	}
    ?>

    </select>

<?php if (file_exists("extras/FCKeditor/fckeditor.php")&& get_config("applications/".apptitle($appname)."/b-fcknews","on")!=""){?>

    <input type='button' name='insert' value='inserisci in intestazione messaggio' onclick="
    var oEditor = FCKeditorAPI.GetInstance('head') ;
    var index= images.image.selectedIndex;
    if (index==-1)index=0;
    var name = document.images.image.options[index].text;
    var html= '<img src=\'<?php echo base_path()."$pic_path/";?>'+name+'\'/>\n';
    if (name) {
	    oEditor.InsertHtml(html);
    }
    " />

<?php }else{ ?>
    <input type='button' name='insert' value='inserisci in intestazione messaggio' onclick="
        var oEditor = document.insnews.head;
    var index= images.image.selectedIndex;
    if (index==-1)index=0;
    var name = document.images.image.options[index].text;
    var html= '[img]<?php echo base_path()."$pic_path/";?>'+name+'[/img]\n';
    if (name) {
	var before = oEditor.value.substring(0, oEditor.selectionStart);
	var sel    = oEditor.value.substring(oEditor.selectionStart, oEditor.selectionEnd);
	var after  = oEditor.value.substring(oEditor.selectionEnd, oEditor.textLength);
	oEditor.value = before + html + after;
    }
    " />
<?php } ?>

<?php if (file_exists("extras/FCKeditor/fckeditor.php")&& get_config("applications/".apptitle($appname)."/b-fcknews","on")!=""){?>

    <input type='button' name='insert' value='inserisci in corpo messaggio' onclick="
    var oEditor = FCKeditorAPI.GetInstance('body') ;
    var index= images.image.selectedIndex;
    if (index==-1)index=0;
    var name = document.images.image.options[index].text;
    var html= '<img src=\'<?php echo base_path()."$pic_path/";?>'+name+'\'/>\n';
    if (name) {
	    oEditor.InsertHtml(html);
    }
    " />

<?php }else{ ?>
    <input type='button' name='insert' value='inserisci in corpo messaggio' onclick="
        var oEditor = document.insnews.body;
    var index= images.image.selectedIndex;
    if (index==-1)index=0;
    var name = document.images.image.options[index].text;
    var html= '[img]<?php echo base_path()."$pic_path/";?>'+name+'[/img]\n';
    if (name) {
	var before = oEditor.value.substring(0, oEditor.selectionStart);
	var sel    = oEditor.value.substring(oEditor.selectionStart, oEditor.selectionEnd);
	var after  = oEditor.value.substring(oEditor.selectionEnd, oEditor.textLength);
	oEditor.value = before + html + after;
    }
    " />
<?php } ?>
    <input type='submit' name='delete' value='elimina' />
    <hr />
    </form>
<?php
	}
    }
}
//-------------------------------------------------------------------------------------------------------------------------

function exec_preview_news($news,$path,$avatar_path,$appname){
    if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()){
	admin_writelog("news:preview news");
	$dataora=mktime( stripslashes($_POST['data_ora']), stripslashes($_POST['data_minuto']), 0, stripslashes($_POST['data_mese']), stripslashes($_POST['data_giorno']), stripslashes($_POST['data_anno']));
	$head=stripslashes($_POST['head']);
	if (defined('LIBBBCODE'))$head=bb2html($head);
	if (defined('LIBEMOTICONS'))$head=emoticons2html($head);

	$body=stripslashes($_POST['body']);
	$title=stripslashes(htmlspecialchars($_POST['title']));
	$avatar=stripslashes(htmlspecialchars($_POST['icon']));
	$head=str_replace("\n","<br />",$head);
	$body=str_replace("\n","<br />",$body);
	if (defined('LIBBBCODE'))$body=bb2html($body);
	if (defined('LIBEMOTICONS'))$body=emoticons2html($body);

	$ntitle="$title";

	echo "<table id='news' class='class_table'>";
	echo "<td colspan='2'><h3>$ntitle</h3></td>";
	echo "<tr>";
	echo "<td width=10><img border='1' alt='$avatar' src='$avatar_path/$avatar'></td>";
	echo "<td>$head</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td colspan='2'>$body</td>";
	echo "</tr>";
	echo "</table><br/>";

	echo "<center>"._POSTATO." ".date(formato_data_ora(),$dataora)." </center>";

	echo "<h3 class='edit'>";
	if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()){
	    echo "<a href=\"javascript:history.back();\">"._INDIETRO."</a>";
	}
	echo "</h3>";
	echo"<hr />";
    }
}
//-------------------------------------------------------------------------------------------------------------------------

function exec_create_news($news,$path,$avatar_path,$appname){
    if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()){
	admin_writelog("news:create news");
	$dataora=mktime( stripslashes($_POST['data_ora']), stripslashes($_POST['data_minuto']), 0, stripslashes($_POST['data_mese']), stripslashes($_POST['data_giorno']), stripslashes($_POST['data_anno']));
	$head=stripslashes($_POST['head']);
	$body=stripslashes($_POST['body']);
	$title=stripslashes(htmlspecialchars($_POST['title']));
	$avatar=stripslashes(htmlspecialchars($_POST['icon']));

	$head=str_replace("<script","",$head);
	$head=str_replace("</script","",$head);
	$head=str_replace("<?","",$head);
	$head=str_replace("?>","",$head);
	$head=str_replace("\r","",$head);
	$head=str_replace("\n","<br>",$head);
	if (defined('LIBBBCODE'))$head=bb2html($head);
	if (defined('LIBEMOTICONS'))$head=emoticons2html($head);

	$body=str_replace("\r","",$body);
	$body=str_replace("\n","<br>",$body);
	$body=str_replace("<script","",$body);
	$body=str_replace("</script","",$body);
	$body=str_replace("<?","",$body);
	$body=str_replace("?>","",$body);
	if (defined('LIBBBCODE'))$body=bb2html($body);
	if (defined('LIBEMOTICONS'))$body=emoticons2html($body);

	$fp=fopen("$news/$dataora.xml","w");
	fwrite($fp,"<?xml version='1.0'?>
<!DOCTYPE fn:news SYSTEM \" news.dtd\">
<fn:news xmlns:fn=\"http://flatnuke.sourceforge.net/news\">
	<fn:title>$title</fn:title>
	<fn:avatar>$avatar</fn:avatar>
	<fn:reads>0</fn:reads>
	<fn:header>$head</fn:header>
	<fn:body>$body</fn:body>
	</fn:news>");

	fclose($fp);

	echo "Notizia inserita<br />";
	echo "<h3 class='edit'><a href='index.php?appname=$appname'>"._CONTINUA."</a></h3>";
	echo"<hr />";
	httplog();
	die();

    }

}
//-------------------------------------------------------------------------------------------------------------------------

function modify_news($path,$avatar_path,$appname){
	$apptitle=apptitle($appname);
	$news=$_GET['file'];
	$news=str_replace("..","",$news);

	$string=join(file("datas/$path/$apptitle/news/$news"));

 	print "<br>";
	$title=ereg_replace(".*<fn:title>","",$string);
	$title=ereg_replace("</fn:title>.*","",$title);

	$header=ereg_replace(".*<fn:header>","",$string);
	$header=ereg_replace("</fn:header>.*","",$header);
	if (defined('LIBEMOTICONS'))$header=html2emoticons($header);
	if (defined('LIBBBCODE'))$header=html2bb($header);


	$avatar=ereg_replace(".*<fn:avatar>","",$string);
	$avatar=ereg_replace("</fn:avatar>.*","",$avatar);

	$body=ereg_replace(".*<fn:body>","",$string);
	$body=ereg_replace("</fn:body>.*","",$body);
	if (defined('LIBEMOTICONS'))$body=html2emoticons($body);
	if (defined('LIBBBCODE'))$body=html2bb($body);

	if (substr_count($string,"<fn:comments>")!=0){
	    $comments=ereg_replace(".*<fn:comments>","",$string);
	    $comments=ereg_replace("</fn:comments>.*","",$comments);
	}

    echo "
<form name='modify_news' action=\"index.php?appname=$appname&op=exec_modify_news\" method=\"POST\" >
<input type=\"hidden\" name=\"mod\" value=\"signews\" />
<input type=\"hidden\" name=\"name\" value=\"$news\" />
<b>"._TITNOTIZIA."</b><br />
<input type=\"text\" name=\"title\" value=\"$title\" /><br /><br />
<b>"._ARGOMENTO."</b><br />
<img name=\"avatar\" src=\"$avatar_path/$avatar\" border=\"0\" alt=\"$avatar_path/$avatar\" />
<br />
<select name=\"icon\" onChange='document.modify_news.avatar.src=\"$avatar_path/\"+this.options[this.selectedIndex].value'>
<option value='$avatar'>".ereg_replace("\..*","",$avatar)."</option>";
$handle=opendir($avatar_path);
while ($file = readdir($handle)) {
	if (!( $file=="." or $file==".." )) {
		$mymodlist .= "$file ";
	}
}
closedir($handle);
$mymodlist = explode(" ", $mymodlist);
sort($mymodlist);
for ($i=1; $i < sizeof($mymodlist); $i++) {
	echo "<option value=\"$mymodlist[$i]\">".ereg_replace("\..*","",$mymodlist[$i])."</option>\n";
}
echo"</select>";

echo "
<br /><br />
<b>"._INTMESSAGGIO."</b><br />";
if (file_exists("extras/FCKeditor/fckeditor.php")&& get_config("applications/".apptitle($appname)."/b-fcknews","on")!=""){

    $sBasePath ="extras/FCKeditor/" ;

    $oFCKeditorhead = new FCKeditor('head') ;
    $oFCKeditorhead->BasePath	= $sBasePath;
    $oFCKeditorhead->Height= 400;
    $oFCKeditorhead->Value= $header;
    $oFCKeditorhead->Create() ;
}
else{
    if (defined('LIBBBCODE'))creatoolbar("document.modify_news.head");
    echo "
    <textarea cols=\"50\" rows=\"7\" name=\"head\">$header</textarea><br /><br />";
}
echo "
<b>"._CORPOMESSAGGIO."</b> <br />";

if (file_exists("extras/FCKeditor/fckeditor.php")&& get_config("applications/".apptitle($appname)."/b-fcknews","on")!=""){

    $oFCKeditorbody = new FCKeditor('body') ;
    $oFCKeditorbody->BasePath	= $sBasePath;
    $oFCKeditorbody->Height= 400;
    $oFCKeditorbody->Value= $body;;
    $oFCKeditorbody->Create() ;
}
else{
    if (defined('LIBBBCODE'))creatoolbar("document.modify_news.body");
    echo "
    <textarea cols=\"50\" rows=\"10\" name=\"body\">$body</textarea>\n";
}

echo "<input type=\"hidden\" name=\"mionome\" value=\"".$comfrom."\"><br /><br />\n";
echo "<input type=\"hidden\" name=\"comments\" value=\"".$comments."\"><br /><br />\n";
echo "<input type=\"submit\" value=\""._INSNOTIZIA."\">\n";
echo "</form>\n";

    $pic_path="datas/$path/".apptitle($appname)."/newsimages";
    if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()){
	$subop=$_GET['subop'];
	if ($subop=="upload"){
	    if (move_uploaded_file($_FILES['image']['tmp_name'], $pic_path."/".$_FILES['image']['name'])) {
		//echo $_FILES['image']['name'];
		if (strtolower(substr($_FILES['image']['name'],-4))==".php" )
		    unlink($pic_path."/".$_FILES['image']['name']);
		else{
		    echo "<script>alert('immagine ".$_FILES['image']['name']." caricata');</script>";
		}
	    }
	}
	if ($subop=="delimage"){
	    $image=$_POST['image'];
	    if (file_exists($pic_path."/".$image)){
		unlink($pic_path."/".$image);
		echo "<script>alert('immagine $image eliminata');</script>";
	    }
	}
?>

    <hr />
    <form name="images_upload" action="index.php?appname=<?=$appname?>&op=create_news&subop=upload" enctype='multipart/form-data' method='post'>
    carica un immagine<input type="file" name="image"><input type=submit value="load">
    </form>

    <form name="images" action="index.php?appname=<?=$appname?>&op=create_news&subop=delimage" enctype='multipart/form-data' method='POST'>
    <select name="image">

    <?php
	//library auto load
	$i=0;
	$fd=opendir("$pic_path");
	while (false !== ($nf= readdir($fd))){
	    if ($nf[0]!='.' )
		$images[$i++]=$nf;
	}
	closedir($fd);
	if ($i>0){
	    sort($images);
	    foreach($images as $image){
		echo"<option>$image</option>";
	    }
	}
    ?>

    </select>

<?php if (file_exists("extras/FCKeditor/fckeditor.php")&& get_config("applications/".apptitle($appname)."/b-fcknews","on")!=""){?>

    <input type='button' name='insert' value='inserisci in intestazione messaggio' onclick="
    var oEditor = FCKeditorAPI.GetInstance('head') ;
    var index= images.image.selectedIndex;
    if (index==-1)index=0;
    var name = document.images.image.options[index].text;
    var html= '<img src=\'<?php echo base_path()."$pic_path/";?>'+name+'\'/>\n';
    if (name) {
	    oEditor.InsertHtml(html);
    }
    " />

<?php }else{ ?>
    <input type='button' name='insert' value='inserisci in intestazione messaggio' onclick="
        var oEditor = document.insnews.head;
    var index= images.image.selectedIndex;
    if (index==-1)index=0;
    var name = document.images.image.options[index].text;
    var html= '[img]<?php echo base_path()."$pic_path/";?>'+name+'[/img]\n';
    if (name) {
	var before = oEditor.value.substring(0, oEditor.selectionStart);
	var sel    = oEditor.value.substring(oEditor.selectionStart, oEditor.selectionEnd);
	var after  = oEditor.value.substring(oEditor.selectionEnd, oEditor.textLength);
	oEditor.value = before + html + after;
    }
    " />
<?php } ?>

<?php if (file_exists("extras/FCKeditor/fckeditor.php")&& get_config("applications/".apptitle($appname)."/b-fcknews","on")!=""){?>

    <input type='button' name='insert' value='inserisci in corpo messaggio' onclick="
    var oEditor = FCKeditorAPI.GetInstance('body') ;
    var index= images.image.selectedIndex;
    if (index==-1)index=0;
    var name = document.images.image.options[index].text;
    var html= '<img src=\'<?php echo base_path()."$pic_path/";?>'+name+'\'/>\n';
    if (name) {
	    oEditor.InsertHtml(html);
    }
    " />

<?php }else{ ?>
    <input type='button' name='insert' value='inserisci in corpo messaggio' onclick="
        var oEditor = document.insnews.body;
    var index= images.image.selectedIndex;
    if (index==-1)index=0;
    var name = document.images.image.options[index].text;
    var html= '[img]<?php echo base_path()."$pic_path/";?>'+name+'[/img]\n';
    if (name) {
	var before = oEditor.value.substring(0, oEditor.selectionStart);
	var sel    = oEditor.value.substring(oEditor.selectionStart, oEditor.selectionEnd);
	var after  = oEditor.value.substring(oEditor.selectionEnd, oEditor.textLength);
	oEditor.value = before + html + after;
    }
    " />
<?php } ?>

    <input type='submit' name='delete' value='elimina' />
    <hr />
    </form>
   <?php
  }
}
//-------------------------------------------------------------------------------------------------------------------------

function exec_modify_news($news,$path,$avatar_path,$appname){
    if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()){
	$apptitle=apptitle($appname);
	admin_writelog("news:modify news");

	$name=$_POST['name'];
	$head=stripslashes($_POST['head']);
	$body=stripslashes($_POST['body']);
	$title=stripslashes(htmlspecialchars($_POST['title']));
	$avatar=stripslashes(htmlspecialchars($_POST['icon']));
	$comments=stripslashes($_POST['comments']);

	$head=str_replace("<script","",$head);
	$head=str_replace("</script","",$head);
	$head=str_replace("<?","",$head);
	$head=str_replace("?>","",$head);
	$head=str_replace("\r","",$head);
	$head=str_replace("\n","<br>",$head);
	if (defined('LIBBBCODE'))$head=bb2html($head);
	if (defined('LIBEMOTICONS'))$head=emoticons2html($head);

	$body=str_replace("\r","",$body);
	$body=str_replace("\n","<br>",$body);
	$body=str_replace("<script","",$body);
	$body=str_replace("</script","",$body);
	$body=str_replace("<?","",$body);
	$body=str_replace("?>","",$body);
	if (defined('LIBBBCODE'))$body=bb2html($body);
	if (defined('LIBEMOTICONS'))$body=emoticons2html($body);

	$fp=fopen("datas/applications/$apptitle/news/$name","w");
	fwrite($fp,"<?xml version='1.0'?>
<!DOCTYPE fn:news SYSTEM \" news.dtd\">
<fn:news xmlns:fn=\"http://flatnuke.sourceforge.net/news\">
<fn:title>$title</fn:title>
<fn:avatar>$avatar</fn:avatar>
<fn:reads>0</fn:reads>
<fn:header>$head</fn:header>
<fn:body>$body</fn:body>
<fn:comments>
$comments
</fn:comments>
</fn:news>");

	fclose($fp);

	echo "Notizia modificata<br />";
	echo "<h3 class='edit'><a href='index.php?appname=$appname'>"._CONTINUA."</a></h3>";
	echo"<hr />";
	httplog();
	die();

    }

}
//-------------------------------------------------------------------------------------------------------------------------

function delete_news($path,$avatar_path,$appname){
    if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()){
        $apptitle=apptitle($appname);
	admin_writelog("news:delete news");
	$apptitle=apptitle($appname);
	$news="datas/applications/$apptitle/news/".$_GET['file'];
	$news=str_replace("..","",$news);
	unlink("$news");
	echo "Notizia eliminata<br />";
	echo "<h3 class='edit'><a href='index.php?appname=$appname'>"._CONTINUA."</a></h3><hr />";

	echo"<hr />";
    }
}
//-------------------------------------------------------------------------------------------------------------------------

function view_news($path,$avatar_path,$appname){
	$apptitle=apptitle($appname);
	$name=$_GET['file'];
	$name=str_replace("..","",$name);
	if(!file_exists("datas/$path/$apptitle/news/$name"))return;

	$string=join(file("datas/$path/$apptitle/news/$name"));
	

 	print "<br>";
	$title=ereg_replace(".*<fn:title>","",$string);
	$title=ereg_replace("</fn:title>.*","",$title);

	$header=ereg_replace(".*<fn:header>","",$string);
	$header=ereg_replace("</fn:header>.*","",$header);

	$reads=ereg_replace(".*<fn:reads>","",$string);
	$reads=ereg_replace("</fn:reads>.*","",$reads);

	$avatar=ereg_replace(".*<fn:avatar>","",$string);
	$avatar=ereg_replace("</fn:avatar>.*","",$avatar);

	$body=ereg_replace(".*<fn:body>","",$string);
	$body=ereg_replace("</fn:body>.*","",$body);
	$body=urldecode($body);

	if (strpos($string,"<fn:comments>")){
	    $comments=ereg_replace(".*<fn:comments>","",$string);
	    $comments=ereg_replace("</fn:comments>.*","",$comments);

	    $comment= explode("</fn:comment>",$comments);
	    array_splice ($comment, count($comment)-1);

	}

	$ntitle="$title";

	echo "<table id='news' class='class_table'>";
	echo "<td colspan='2'><h3>$ntitle</h3></td>";
	echo "<tr>";
	echo "<td width=10><img border='1' alt='$avatar' src='$avatar_path/$avatar'></td>";
	echo "<td>$header</td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td colspan='2'>$body</td>";
	echo "</tr>";
	echo "</table><br/>";

	echo "<center>"._POSTATO." ".date(formato_data_ora(),substr($name,-14,-4))." </center>";

	echo "<h3 class='edit'>";
	echo "<a href=\"".ahah_link("index.php?appname=$appname&op=comment_news&file=$name", $path,"center")."\">"._COMMENTI."?</a> | ";
	if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()){
	echo "<a href=\"".ahah_link("index.php?appname=$appname&op=delete_news&file=$name", $path,"center")."\">"._ELIMINA."</a> |";
	echo "<a href=\"".ahah_link("index.php?appname=$appname&op=modify_news&file=$name", $path,"center")."\">"._MODIFICA."</a> ";
	}
	echo "</h3>";
	echo"<hr />";

	//visualizza i commenti
	for($i=0; $i<count($comment);$i++){
	    $by=ereg_replace(".*<fn:by>","",$comment[$i]);
	    $by=ereg_replace("</fn:by>.*","",$by);

	    $date=ereg_replace(".*<fn:date>","",$comment[$i]);
	    $date=ereg_replace("</fn:date>.*","",$date);

	    $post=ereg_replace(".*<fn:post>","",$comment[$i]);
	    $post=ereg_replace("</fn:post>.*","",$post);

	    echo"
	     "._DA." <b>$by</b>  "._DATA." <i>".date(formato_data_ora(),$date)."</i>  <br /><br />
	    $post <br />
	    <hr />
	    ";
	    if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()){
		echo "<h3 class='edit'>";
		echo "<a href=\"index.php?appname=$appname&file=$name&num_comment=$i&op=modify_comment_news\">"._MODCOMM."</a>";
		echo " | ";
		echo "<a href=".ahah_link("index.php?appname=$appname&file=$name&num_comment=$i&op=delete_comment_news",$path,"center").">"._DELCOMM."</a>";
		echo "</h3>";
		echo "<hr/>";
	    }
	}




	$reads++;

//aggiorna il contatore cioe sovrascrivi la news
	$fp=fopen("datas/applications/$apptitle/news/$name","w");
	fwrite($fp,"<?xml version='1.0'?>
<!DOCTYPE fn:news SYSTEM \" news.dtd\">
<fn:news xmlns:fn=\"http://flatnuke.sourceforge.net/news\">
	<fn:title>$title</fn:title>
	<fn:avatar>$avatar</fn:avatar>
	<fn:reads>$reads</fn:reads>
	<fn:header>$header</fn:header>
	<fn:body>$body</fn:body>
	<fn:comments>
	    $comments
	</fn:comments>

	</fn:news>");

	fclose($fp);


}
//-------------------------------------------------------------------------------------------------------------------------

//visualizza l'elenco delle newws
function archive_news($path,$avatar_path,$appname){
    $apptitle=apptitle($appname);
    echo "<h3>"._ARCHIVE_NEWS."</h3><br/><br/>";

    //inserisce nell'array $archive tutte le news divise per argomento
    $handle=opendir("datas/$path/$apptitle/news");
    while (false !== ($nf=readdir($handle))){
	if ($nf[0]!='.' ){
	    $xml=join(file("datas/$path/$apptitle/news/$nf"));
	    $archive_arg["$nf"]= get_xml_element("fn:avatar",$xml);
	    $archive_title["$nf"] = get_xml_element("fn:title",$xml);
	}
    }
    closedir($handle);


    $handle=opendir($avatar_path);
    while (false !== ($nf=readdir($handle))){
	if ($nf[0]!='.' ){
	    if (in_array($nf,$archive_arg)){
		$_nf=explode(".",$nf);
		$voce="<table class='class_table' border='1'><tr><td><a onclick=\"javascript: if (document.getElementById('$nf').style.display!='inline')document.getElementById('$nf').style.display='inline';else document.getElementById('$nf').style.display='none';\">";
		$voce.="<img src='$avatar_path/$nf' width='32'><strong><u>".$_nf[0]."</u></strong ><img src='themes/".theme()."/images/down.png'  />";
		$voce.="</a><br/>\n";
		$voce.="<div id='$nf' style=\"display:none;\">";
		foreach($archive_arg as $name =>$arg){
		    if ($arg==$nf){
			$voce.="<a href=\"".ahah_link("index.php?appname=$appname&op=view_news&file=$name",$path,"center")."\">".$archive_title[$name]."</a><br/>\n";
		    }
		}
		$voce.="</div></td></tr></table><hr/>\n";
		$argomenti[$i++]=$voce;
	    }
	}
    }
    closedir($handle);
    sort($argomenti);
    foreach ($argomenti as $argomento){
	echo "$argomento</br>\n";

    }
}
//-------------------------------------------------------------------------------------------------------------------------

function export_newsRSS($path,$avatar_path,$appname){
    $apptitle=apptitle($appname);
    create_newsRSS("datas/$path/$apptitle/news");
    echo "<a href='datas/applications/$apptitle/backend.xml'>"._PRELEVA_FILE_RSSRDF."</a>\n";
}
//-------------------------------------------------------------------------------------------------------------------------

function news_list($news,$path,$avatar_path,$appname){
    $page=$_GET['page'];
    if ($page=="")$page=0;

    $handle=opendir($news);
    $i=0;
    while ($file = readdir($handle)) {
	if (substr($file,-4)==".xml") {
		$newslist[$i++]=$file;

	}
    }
    closedir($handle);

    if (count($newslist)>1)rsort($newslist);

    echo "<b>"._PAGINE."</b>";
    for ($i=0;$i<(count($newslist)/newsPP($path,apptitle($appname)));$i++){
	if ($i!=$page)
	    echo " <a href=".ahah_link("index.php?appname=$appname&page=$i",$path,"center").">".($i+1)."</a> ";
	else
	    echo " ".($i+1)." ";
    }
    echo "<br />";

    $startpost=$page*newsPP($path,apptitle($appname));
    for ($i=0; $i < count($newslist); $i++) {
	if (substr($newslist[$i],0,-4) < strtotime("now") || (defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()){
	    if ($i>=$startpost && $i<$startpost+newsPP($path,apptitle($appname))){
		$string=join(file("$news/".$newslist[$i]));

		print "<br>";
		$title=ereg_replace(".*<fn:title>","",$string);
		$title=ereg_replace("</fn:title>.*","",$title);

		$header=ereg_replace(".*<fn:header>","",$string);
		$header=ereg_replace("</fn:header>.*","",$header);

		$reads=ereg_replace(".*<fn:reads>","",$string);
		$reads=ereg_replace("</fn:reads>.*","",$reads);

		$avatar=ereg_replace(".*<fn:avatar>","",$string);
		$avatar=ereg_replace("</fn:avatar>.*","",$avatar);

		$ntitle="$title";

		echo "<table id='news' class='class_table'>";
		echo "<td colspan='2'><h3>$ntitle</h3></td>";
		echo "<tr>";
		echo "<td width=10><img border='1' alt='$avatar' src='$avatar_path/$avatar'></td>";
		echo "<td>$header</td>";
		echo "</tr>";
		echo "</table><br/>";

		echo "<center>";
		if (substr($newslist[$i],0,-4) >= strtotime("now"))echo "<font color=#ff0000>\n";
		echo ""._POSTATO." ".date(formato_data_ora(),substr($newslist[$i],0,-4))." ";
		if (substr($newslist[$i],0,-4) >= strtotime("now"))echo " - "._NON_VISIBILE." </font>\n";
		echo "( Letta $reads volte | commenti ".count_comment_news("$news/".$newslist[$i])." )";
		echo "</center>";

		echo "<h3 class='edit'>";
		echo "<a href=\"".ahah_link("index.php?appname=$appname&op=view_news&file=".$newslist[$i],$path,"center")."\">"._LEGGITUTTO."</a> | ";
		echo "<a href=\"".ahah_link("index.php?appname=$appname&op=comment_news&file=".$newslist[$i],$path,"center")."\">"._COMMENTI."?</a> | ";
		if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()){
		    echo "<a href=\"".ahah_link("index.php?appname=$appname&op=delete_news&file=".$newslist[$i],$path,"center")."\">"._ELIMINA."</a> |";
		    echo "<a href=\"".ahah_link("index.php?appname=$appname&op=modify_news&file=".$newslist[$i],$path,"center")."\">"._MODIFICA."</a> ";
		}
		echo "</h3>";
		echo"<hr />";
	    }
	}
    }
    echo "<b>"._PAGINE."</b>";
    for ($i=0;$i<(count($newslist)/newsPP($path,apptitle($appname)));$i++){
	if ($i!=$page)
	    echo " <a href=".ahah_link("index.php?appname=$appname&page=$i",$path,"center").">".($i+1)."</a> ";
	else
	    echo " ".($i+1)." ";
    }
    echo "<br />";
    if ((defined('LIBGROUP') && group_getusergroup("webmaster",user_getuser())) || admin_isadmin()){
	echo "<h3 class='edit'>\n";
	echo "<a href=".ahah_link("index.php?appname=$appname&op=create_news",$path,"center").">"._ADDNEWS."</a>\n";
	echo "</h3>\n";
    }
}
//-------------------------------------------------------------------------------------------------------------------------

// first installation
/* news Page */
//------------------------------------------------------------------------------------------------------------------------

// Configure - News
if (!file_exists("datas/$path/$apptitle"))mkdir("datas/$path/$apptitle",0755);
if (!file_exists("datas/$path/$apptitle/newsimages"))mkdir("datas/$path/$apptitle/newsimages",0755);

set_config_folder("applications/".apptitle($appname));
if (file_exists("extras/FCKeditor/fckeditor.php")&& get_config("applications/".apptitle($appname)."/b-fcknews","on")!=""){
    include("extras/FCKeditor/fckeditor.php");
}

//-------------------------------------------------------------------------------------------------------------------------
//make all directory and copy the arguments icons
if (!file_exists("datas/$path"))mkdir("datas/$path",0755);
if (!file_exists("datas/$path/$apptitle"))mkdir("datas/$path/$apptitle",0755);
if (!file_exists("datas/$path/$apptitle/news"))mkdir("datas/$path/$apptitle/news",0755);
if (!file_exists("datas/$path/$apptitle/arguments")){
    mkdir("datas/$path/$apptitle/arguments",0755);
    copytree("$path/$appname/images","datas/$path/$apptitle/arguments");
}



?>
<div class='table'>
<p>
<?php
    $op=$_GET['op'];
    switch($op){
	case "admin":
	    admin($path,$appname);
	    break;
	case "exec_admin":
	    exec_admin($path,$appname);
	    break;
	case "create_news":
	    create_news("datas/$path/$apptitle/news",$path,"datas/$path/$apptitle/arguments",$appname);
	    break;
	case "exec_preview_news":
	    exec_preview_news("datas/$path/$apptitle/news",$path,"datas/$path/$apptitle/arguments",$appname);
	    break;
	case "exec_create_news":
	    exec_create_news("datas/$path/$apptitle/news",$path,"datas/$path/$apptitle/arguments",$appname);
	    break;
	case "delete_news":
	    delete_news($path,"datas/$path/$apptitle/arguments",$appname);
	    break;
	case "modify_news":
	    modify_news($path,"datas/$path/$apptitle/arguments",$appname);
	    break;
	case "exec_modify_news":
	    exec_modify_news("datas/$path/$apptitle/news",$path,"datas/$path/$apptitle/arguments",$appname);
	    break;
	case "comment_news":
	    comment_news("datas/$path/$apptitle/news",$path,"datas/$path/$apptitle/arguments",$appname);
	    break;
	case "exec_comment_news":
	    exec_comment_news("datas/$path/$apptitle/news",$path,"datas/$path/$apptitle/arguments",$appname);
	    break;
	case "modify_comment_news":
	    modify_comment_news("datas/$path/$apptitle/news",$path,"datas/$path/$apptitle/arguments",$appname);
	    break;
	case "exec_modify_comment_news":
	    exec_modify_comment_news("datas/$path/$apptitle/news",$path,"datas/$path/$apptitle/arguments",$appname);
	    break;
	case "delete_comment_news":
	    delete_comment_news("datas/$path/$apptitle/news",$path,"datas/$path/$apptitle/arguments",$appname);
	    break;
	case "view_news":
	    view_news($path,"datas/$path/$apptitle/arguments",$appname);
	    break;
	case "archive_news":
	    archive_news($path,"datas/$path/$apptitle/arguments",$appname);
	    break;
	case "export_newsRSS":
	    export_newsRSS($path,"datas/$path/$apptitle/arguments",$appname);
	    break;
	default:
	    news_list("datas/$path/$apptitle/news",$path,"datas/$path/$apptitle/arguments",$appname);
	    break;
    }

    echo "<h3 class='edit'>\n";
    echo "<a href=".ahah_link("index.php?appname=$appname&op=archive_news",$path,"center").">"._ARCHIVE_NEWS."</a>\n";
    echo "<a href=".ahah_link("index.php?appname=$appname&op=export_newsRSS",$path,"center").">"._PRELEVA_FILE_RSSRDF."</a>\n";
    echo "<a href=".ahah_link("index.php?appname=$appname",$path,"center").">"._MAIN."</a>\n";
    echo "</h3>\n";
    if (admin_isadmin()){
	echo "<h3 class='edit' style=\"align:right; \"><a href=".ahah_link("index.php?appname=$appname&op=admin",$path,"center").">"._ADMIN_NEWS."</a></h3>";
    }


?>
</p>
</div>
